/**
 * Copyright (C) 2021 - 2031 O-Cubes Co., Ltd.
 */

/****************************************************************
 *  @file    image_header.h
 *  @brief   Image header file
 *  @version v1.0
 *  @date    03. Apr. 2023
 ****************************************************************/

#ifndef __IMAGE_HEADER_H__
#define __IMAGE_HEADER_H__

#include <stdint.h>
#include <string.h>
#include "common.h"
#include "mem_map_table.h"

#ifdef __cplusplus
extern "C" {
#endif

#define OCUBES_MAGIC            0x4F434253   /* Magic number   */
#define VER_LEN                 48           /* Version buffer length */

#define PKG_HDR_FLASH_BASE      MEM_MAP_FLASH_BASE_ADDR   /* pkg header flash base address   */
#define PKG_HDR_FLASH_SIZE      0x1000                    /* pkg header flash size   */

typedef enum {
	eBOOT_PARTITION1,
	eBOOT_PARTITION2,
	eBOOT_PARTITION_UNKNOWN,
} boot_partition_enum;

typedef enum {
	eIMG_SDK_PART1,
	eIMG_SDK_PART2,
	eIMG_MAX_NUM,
} img_type_enum;

typedef enum {
	eSOC_EOCV100,
	eSOC_UNKNOWN,
} soc_type_enum;

typedef enum {
	eBOARD_TYPE_FPGA,
	eBOARD_TYPE_EVB,
	eBOARD_TYPE_SI,
	eBOARD_TYPE_DEMO_PUB,
	eBOARD_TYPE_DEMO_NTS,
	eBOARD_TYPE_UNKNOWN,
} board_type_enum;

struct subimg_header {
	uint32_t img_type;         /* sub_image type */
	uint32_t img_size;         /* sub_image size */
	uint32_t img_offset;       /* sub_image offset */
	uint32_t part_size;        /* sub_image flash partition size */
	uint32_t part_base;        /* sub_image flash base address */
	uint32_t img_crc32;        /* CRC checksum of sub_image */
} __packed;

struct  pkg_header {
	uint32_t magic;           /* Image header magic number */
	uint32_t hdr_size;        /* Image header size in bytes */
	uint32_t hdr_crc32;       /* Image header CRC checksum */
	uint32_t time;            /* Image creation timestamp*/
	uint32_t data_size;       /* Image data size in bytes */
	uint32_t data_crc32;      /* Image data CRC checksum */
	uint32_t soc_type;        /* Soc type */
	uint32_t board_type;      /* board type */
	uint32_t boot_partition;  /* Boot Partition */
	struct subimg_header subimg_hdr[eIMG_MAX_NUM]; /* Subimage header */
	uint8_t  version[VER_LEN];/*Image version*/
} __packed;

uint32_t ntohl(uint32_t a);
uint32_t htonl(uint32_t a);
int pkg_header_check(struct pkg_header *hdr);
int subimg_data_check(uint8_t *data_addr, uint32_t data_size, uint32_t data_crc32);

#ifdef __cplusplus
}
#endif

#endif /* __IMAGE_HEADER_H__ */

